package cm.studapp;

import cm.studapp.database.DbManager;
import cm.studapp.helpers.ButtonHelper;
import cm.studapp.helpers.TimeHelper;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

public class AgendaToday extends Activity {

	LinearLayout parentLayout;
	DbManager db;
	Cursor appointmentsData;
	TimeHelper th;
	ButtonHelper bHelper;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.agenda_today_main);

        initComponents();
        initViews();
    }

	private void initComponents() {
		// TODO Auto-generated method stub
		db=new DbManager(this);
		th=new TimeHelper();
		bHelper = new ButtonHelper(this);
		appointmentsData=db.getTodayEvents(th.getTodayInMillis());
	}

	private void initViews() {
		// TODO Auto-generated method stub
		parentLayout=(LinearLayout)findViewById(R.id.AgnTodayLL_parent);
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
			    LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		params.topMargin = 1;
		params.height = 76;
		
		//sonDay.setLayoutParams(paramSonDay);
		while(appointmentsData.moveToNext()){
			int idAct = appointmentsData.getInt(0);
			String eventName=appointmentsData.getString(1);
			long startTime = appointmentsData.getLong(8);
			long endTime = appointmentsData.getLong(9);
			String startHour=th.millisToHour(appointmentsData.getLong(1));
			RelativeLayout child = (RelativeLayout)getLayoutInflater().inflate(R.layout.agenda_today_elem_rl, null);
			
			TextView title=(TextView)child.getChildAt(0);
			title.setText(eventName);
			
			TextView initHour=(TextView)child.getChildAt(3);
			initHour.setText(th.millisToHour(startTime));
			
			TextView endHour = (TextView)child.getChildAt(1);
			endHour.setText(th.millisToHour(endTime));
			child.setId(idAct);			
			child.setClickable(true);
			child.setOnClickListener(bHelper);
			parentLayout.addView(child, params);	
			
		}
		
		Cursor dutys =db.getTodayDutys(th.getTodayInMillis());
		if(dutys.getCount()>0){
			TextView child = (TextView)getLayoutInflater().inflate(R.layout.agenda_today_elem_title, null);
			parentLayout.addView(child);
		}
			
		while(dutys.moveToNext()){
			String dutyName=dutys.getString(0);
			long startTime = dutys.getLong(1);
			long endTime = dutys.getLong(2);
			
			int dutyType = dutys.getInt(3);
			String belongsTo = dutys.getString(4);
			RelativeLayout child = (RelativeLayout)getLayoutInflater().inflate(R.layout.agenda_today_duty_elem, null);
			
			TextView title=(TextView)child.getChildAt(1);
			title.setText(dutyName);
			
			TextView initHour=(TextView)child.getChildAt(4);
			initHour.setText(th.millisToHour(startTime));
			
			TextView endHour = (TextView)child.getChildAt(2);
			endHour.setText(th.millisToHour(endTime));
			
			TextView ramo = (TextView)child.getChildAt(5);
			ramo.setText("Ramo: "+belongsTo);
			
			TextView type = (TextView)child.getChildAt(0);
			if(dutyType == 0)
				type.setText("Horario de estudio");
			else
				type.setText("Horario para tareas");
			parentLayout.addView(child);	
			
		}
		/*
		RelativeLayout child = (RelativeLayout)getLayoutInflater().inflate(R.layout.agenda_today_elem_rl, null);
		TextView title=(TextView)child.getChildAt(0);
		System.out.println(title.getText());
		*/
		appointmentsData.close();
		dutys.close();

	}
}
